/**
 * 智慧小区管理系统 - 业主房户关联API接口
 * @author Wu.Liang
 * @date 2025-01-30
 */
import api from './request'
import type { ApiResponse } from './request'

// 业主房户关联相关类型定义
export interface OwnerHouseholdData {
  id: number
  ownerId: number
  householdId: number
  ownershipType: number
  ownershipRatio: number
  isPrimaryOwner: boolean
  certificateNumber: string
  certificateDate: string
  mortgageInfo: string
  status: number
  createTime: string
  updateTime: string
  // 关联信息
  communityId: number
  communityName: string
  buildingId: number
  buildingName: string
  unitId: number
  unitName: string
  roomId: number
  roomNumber: string
}

export interface CreateOwnerHouseholdDTO {
  ownerId: number
  householdId: number
  ownershipType: number
  ownershipRatio: number
  isPrimaryOwner: boolean
  certificateNumber: string
  certificateDate: string
  mortgageInfo?: string
}

export interface UpdateOwnerHouseholdDTO {
  ownershipType?: number
  ownershipRatio?: number
  isPrimaryOwner?: boolean
  certificateNumber?: string
  certificateDate?: string
  mortgageInfo?: string
}

export interface OwnerHouseholdQueryDTO {
  ownerId?: number
  householdId?: number
  communityId?: number
  buildingId?: number
  unitId?: number
  ownershipType?: number
  isPrimaryOwner?: boolean
  status?: number
  pageNum?: number
  pageSize?: number
}

// 业主房户关联API接口
export const ownerHouseholdApi = {
  /**
   * 获取业主房户关联列表
   * @author Wu.Liang
   */
  getOwnerHouseholdList: (params: OwnerHouseholdQueryDTO) => 
    api.get<ApiResponse<OwnerHouseholdData[]>>('/api/region/owner-households', { params }),

  /**
   * 获取业主房户关联分页列表
   * @author Wu.Liang
   */
  getOwnerHouseholdPage: (params: OwnerHouseholdQueryDTO) => 
    api.get<ApiResponse<any>>('/api/region/owner-households', { params }),

  /**
   * 根据业主ID获取房户关联列表
   * @author Wu.Liang
   */
  getOwnerHouseholds: (ownerId: number) => 
    api.get<ApiResponse<OwnerHouseholdData[]>>(`/api/region/owner/household/owner/${ownerId}`),

  /**
   * 根据房户ID获取业主关联列表
   * @author Wu.Liang
   */
  getHouseholdOwners: (householdId: number) => 
    api.get<ApiResponse<OwnerHouseholdData[]>>(`/api/region/households/${householdId}/owners`),

  /**
   * 获取业主房户关联详情
   * @author Wu.Liang
   */
  getOwnerHouseholdDetail: (id: number) => 
    api.get<ApiResponse<OwnerHouseholdData>>(`/api/region/owner-households/${id}`),

  /**
   * 创建业主房户关联
   * @author Wu.Liang
   */
  createOwnerHousehold: (data: CreateOwnerHouseholdDTO) => 
    api.post<ApiResponse<number>>('/api/region/owner/household', data),

  /**
   * 更新业主房户关联
   * @author Wu.Liang
   */
  updateOwnerHousehold: (id: number, data: UpdateOwnerHouseholdDTO) => 
    api.put<ApiResponse<void>>(`/api/region/owner/household/${id}`, data),

  /**
   * 删除业主房户关联
   * @author Wu.Liang
   */
  deleteOwnerHousehold: (id: number) => 
    api.delete<ApiResponse<void>>(`/api/region/owner/household/${id}`),

  /**
   * 解除业主与房户关联
   * @author Wu.Liang
   */
  removeOwnerHousehold: (ownerId: number, householdId: number) => 
    api.delete<ApiResponse<void>>(`/api/region/owners/${ownerId}/households/${householdId}`),

  /**
   * 设置主业主
   * @author Wu.Liang
   */
  setPrimaryOwner: (ownerId: number, householdId: number) => 
    api.put<ApiResponse<void>>(`/api/region/owner/household/owner/${ownerId}/household/${householdId}/primary`),

  /**
   * 检查业主房户关联是否存在
   * @author Wu.Liang
   */
  checkOwnerHouseholdExists: (ownerId: number, householdId: number) => 
    api.get<ApiResponse<boolean>>(`/api/region/owner-households/check`, { 
      params: { ownerId, householdId } 
    }),

  /**
   * 获取房户的主业主
   * @author Wu.Liang
   */
  getPrimaryOwner: (householdId: number) => 
    api.get<ApiResponse<OwnerHouseholdData>>(`/api/region/households/${householdId}/primary-owner`),

  /**
   * 批量创建业主房户关联
   * @author Wu.Liang
   */
  batchCreateOwnerHouseholds: (data: CreateOwnerHouseholdDTO[]) => 
    api.post<ApiResponse<void>>('/api/region/owner-households/batch', data),

  /**
   * 批量删除业主房户关联
   * @author Wu.Liang
   */
  batchDeleteOwnerHouseholds: (ids: number[]) => 
    api.delete<ApiResponse<void>>('/api/region/owner-households/batch', { data: ids }),

  /**
   * 获取业主房户关联统计信息
   * @author Wu.Liang
   */
  getOwnerHouseholdStatistics: (ownerId?: number) => 
    api.get<ApiResponse<any>>('/api/region/owner-households/statistics', { 
      params: { ownerId } 
    })
}
